﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBQLTV;
using BUS;

namespace QuanLyThuVien
{
    public partial class frmMuonSachCT : Form
    {
        public string TenSach;
        public string TheLoai;
        public string TacGia;
        public int MaCuonSach;
        public bool HetSach = false;

        CuonSachBUS cuonSachBUS = new CuonSachBUS();
        private frmMuonSach mainForm = null;
        public frmMuonSachCT(Form callingForm)
        {
            mainForm = callingForm as frmMuonSach;

            InitializeComponent();

            TenSach = "";
            TheLoai = "";
            TacGia = "";
            MaCuonSach = -1;

            List<CUONSACH> listCS = new List<CUONSACH>();
            listCS = cuonSachBUS.GetListCuonSachVoiTinhTrang(cuonSachBUS.TT_TrongTV);

            foreach (CUONSACH cs in listCS)
            {
                if (!comboBox_maCuonSach.Items.Contains(cs.MaCuonSach))
                    comboBox_maCuonSach.Items.Add(cs.MaCuonSach);
            }
            if (listCS.Count == 0)
            {
                HetSach = true;
            }
        }
        private void button_huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void button_luu_Click(object sender, EventArgs e)
        {
            if (!comboBox_maCuonSach.Items.Contains(int.Parse(comboBox_maCuonSach.Text)))
                MessageBox.Show("Mã cuốn sách không tồn tại!", "Lỗi");
            else
            {
                UpdateThongTinTruyen();
                if (mainForm.UpdateDataTable())
                {
                    this.Close();
                }
                else MessageBox.Show("Sách đã được mượn");
            }
        }

        private void UpdateThongTinTruyen()
        {
            TheLoai = InputFormat.formatString(tb_theLoai.Text.ToString());
            TacGia = InputFormat.formatString(tb_tacGia.Text.ToString());
            MaCuonSach = int.Parse(comboBox_maCuonSach.SelectedItem.ToString());
        }
        private void UpdateThongTin()
        {
            MaCuonSach = int.Parse(comboBox_maCuonSach.Text);
            tb_tenSach.Text = cuonSachBUS.GetTenSach(MaCuonSach);
            TenSach = InputFormat.formatString(tb_tenSach.Text.ToString());

            tb_theLoai.Text = cuonSachBUS.GetTenTheLoai(MaCuonSach);

            //tác giả
            List<string> listTenCacTacGia = cuonSachBUS.GetListTacGia(MaCuonSach);
            string tacGia = "";
            foreach (string item in listTenCacTacGia)
            {
                tacGia += item + " , ";
            }
            tb_tacGia.Text = tacGia;
        }
        
        private void comboBox_maCuonSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateThongTin();
        }
        private void comboBox_maCuonSach_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (!comboBox_maCuonSach.Items.Contains(int.Parse(comboBox_maCuonSach.Text)))
                    MessageBox.Show("Mã cuốn sách không tồn tại!", "Lỗi");
                else UpdateThongTinTruyen();
        }
    }
}
